﻿Shader "Dan/UI/UI_Mosaic02"
{
    Properties
    {
        _MainTex ("MainTex", 2D) = "white" {}
        _Color ("Color", Color) = (1,1,1,1)
        _Emission ("Emission", Float ) = 1
        _FadeOut ("FadeOut", Range(0, 1)) = 0.5972567
        _ScanelineContrast ("ScanelineContrast", Range(0, 1)) = 0.5
        _ScanelineDensity ("ScanelineDensity", Float ) = 512
        _ScanelineSpeed("ScanelineSpeed", Float ) = 2
        _Alpha ("Alpha", Range(0, 1)) = 1
        _ShinnySpeed ("ShinnySpeed", Float ) = 1
        _ShinnyStrength ("ShinnyStrength", Range(0, 1)) = 1
        _Mosaic_U ("Mosaic_U", Float ) = 2
        _Mosaic_V ("Mosaic_V", Float ) = 1
        _MosaicDensity ("MosaicDensity", Float ) = 6
        _MosaicMaskSpeed ("MosaicMaskSpeed", Float ) = 0.5
        [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    }
    
    SubShader
    {

        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            Blend SrcAlpha OneMinusSrcAlpha
            Cull Off
            ZWrite Off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

             float4 _Color;
             float _FadeOut;
             float _ShinnySpeed;
             float _Emission;
             sampler2D _MainTex;  
             float4 _MainTex_ST;
             float _ScanelineContrast;
             float _ScanelineDensity;
             float _ScanelineSpeed;
             float _Alpha;
             float _ShinnyStrength;
             float _MosaicDensity;
             float _MosaicMaskSpeed;
             float _Mosaic_U;
             float _Mosaic_V;

            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;        
                return o;
            }
            
            
            float Flick (float2 timeSpeed){
                float2 sk = timeSpeed + 0.2127+timeSpeed.x*0.3713*timeSpeed.y;
                float2 rnd = 4.789*sin(489.123*(sk));
                return frac(rnd.x*rnd.y*(1+sk.x));
            }

            

            //扰动线条
            float ScaneLine01 (float uvdir, float shinnySpeed, float strength, float lineContrast , float lineDensity, float lineSpeed){
                float stime = _Time.y*0.001*shinnySpeed;                
                float shinny = Flick(float2(stime,stime));
                float scaneLine =saturate(lineContrast+abs(sin(uvdir*lineDensity+_Time.y*lineSpeed)));
                float shinnyStrength =saturate(strength +  shinny *(1 - strength));
                return scaneLine*shinnyStrength;
            }

           //马赛克扰动  在表面扰动 带淡入淡出
           float Mosaic02(float2 uv , float density , float mosaicspeed , float mosaicU, float mosaicV, float fade){
                float2 ruv = abs(uv*2.0-1.0);
                float layerUV = floor((1.0 - saturate(saturate(max(ruv.x,ruv.y)))) * density) / (density - 1);
                float speed = _Time.y*0.001*mosaicspeed;
                float2 rd = floor((uv*float2(mosaicU,mosaicV)) * density) / (density - 1)+speed;
                float flick = Flick(rd);
                float mosaic= step(fade,(layerUV+layerUV*flick)/2.0)*flick;
                return smoothstep( 0, fade, mosaic );
            }

            fixed4 frag (v2f i ) : SV_Target
            {
                
                
                float2 uv = i.uv ;
                
            
                float4 mainTex = tex2D(_MainTex,TRANSFORM_TEX(uv, _MainTex));
                float3 emissive = _Color.rgb*_Emission*mainTex.rgb;
                float3 finalColor = emissive;
                float fade = 1 - _FadeOut;
                
                float mosaic = Mosaic02(uv,_MosaicDensity,_MosaicMaskSpeed,_Mosaic_U,_Mosaic_V,fade);
                float scaneLine =ScaneLine01(uv.y,_ShinnySpeed,_ShinnyStrength,_ScanelineContrast,_ScanelineDensity,_ScanelineSpeed);
                
                

                return fixed4(finalColor,(mainTex.a*saturate(mosaic)*scaneLine*_Alpha));
            
                
         
         
                


            

            



            }
            ENDCG
        }
    }
    
    
    
}
